package com.xl.minyue.tc2.until;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xl.minyue.tc2.pojo.TagInfo;
import com.xl.minyue.tc2.service.ReportFormServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ProTest {


    public static void main(String[] args) {

        String num="-234.64789231";
        double aDouble = Double.parseDouble(num);
        System.out.println(aDouble+"<---aDouble");
        //保留2位小数 四舍五入
        String s = String.format("%.2f", aDouble);
        System.out.println(s+"<----sssss-----");

        SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String format = d1.format(new Date());
        String nowTime = d2.format(new Date());
        String st_time=format+" 16:00:00";
        System.out.println(
                nowTime+"-----------"+st_time
        );
        System.out.println(new Date());
        try {
            Date parse = d2.parse(st_time);
            System.out.println();
            long num2 = new Date().getTime() - parse.getTime();
            System.out.println(num2>0);


        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


    public static   List<String> getShowArray(){
        SimpleDateFormat d3 = new SimpleDateFormat("mm");
        SimpleDateFormat d4 = new SimpleDateFormat("HH");
        int minitue = Integer.parseInt(d3.format(new Date()));
        int hour = Integer.parseInt(d4.format(new Date()));
        int num_1=(minitue/5)+1;
        int num2=hour*12+num_1;
        System.out.println(num2);
        List<String> list2=new ArrayList<>();//---->时间\
        for (int i = 0; i < num2; i++) {
            String s2="";
            int j_n=i/12;
            if (j_n<10){
                int num3=5*i-j_n*60;
                if(num3<10){
                    s2="0"+j_n+":0"+num3;
                }else if(num3<60){
                    s2="0"+j_n+":"+num3;
                }
            }else{
                int num3=5*i-j_n*60;
                if(num3<10){
                    s2=j_n+":0"+num3;
                }else if(num3<60){
                    s2=j_n+":"+num3;
                }
            }
            list2.add(s2);
        }
        //System.out.println(JSON.toJSONString(list2));
        return list2;
    }


    public HashMap<String, Object> showReportData2(HttpServletRequest req){
        SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat d3 = new SimpleDateFormat("mm");
        SimpleDateFormat d4 = new SimpleDateFormat("HH");
        SimpleDateFormat d5 = new SimpleDateFormat("HH:mm");
        HashMap<String, Object> resMap = new HashMap<>();
        String format = d1.format(new Date());
        List<String> nowTimeList = ProTest.getShowArray();
        try {
            String data = "";
            JSONObject jsonObject = JSON.parseObject(data);
            JSONArray jsonArray = jsonObject.getJSONArray("codeList");
            String date = jsonObject.getString("date");
            if (date==null || date.equals("")){
                date=format;
            }
            System.out.println(date+"<--------展示数据的日期"+jsonArray);
            List<Object> list = new ArrayList<>();
            if (jsonArray.size()>0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    String code = jsonArray.get(i).toString();
                    String name = null;
                    int num_5 = code.indexOf("?&=");
                    if (num_5 != -1) {
                        name = code.substring(num_5 + "?&=".length(), code.length());
                        code = code.substring(0, num_5);
                    }
                    Map map2 = new HashMap<>();
                    List<String> list2 = new ArrayList<>();
                    List<TagInfo> infoList = new ReportFormServiceImpl().showDataByDateAndPoint2_(code, date);
                    int num9=0;
                    String s3_time="";
                    int a=0;
                    int b=0;
                    List<String> list3 = new ArrayList<>();
                    for (int j = 0; j <infoList.size(); j++) {
                        TagInfo t = infoList.get(j);
                        String colltime = t.getColltime();
                        Date parse = d2.parse(colltime);
                        String hour_minitue = d5.format(parse);
                        /*int minitue = Integer.parseInt(d3.format(parse));
                        int hour= Integer.parseInt(d4.format(parse));
                        if(j==0||j==1||j==(infoList.size()-1)){
                            System.out.println(t.toString()+"<--------"+j);
                        }
                        if(j==0){
                            s3_time=colltime;
                            int floor = (int) Math.floor(minitue / 5);
                            int num5=hour*12+floor;
                            for (int k = 0; k < num5; k++) {
                                list2.add("0.00");
                            }
                            list2.add(String.format("%.2f",Double.parseDouble(t.getTagvalue())));
                        }*/

                        for (int k = b; k < nowTimeList.size(); k++) {
                            if (nowTimeList.get(k).equals(hour_minitue)){
                                a=k;
                                break;
                            }
                        }
                        for (int k2 = b; k2 < a; k2++) {
                            list2.add("0.00");
                            if (k2==(a-1)){
                                b=k2;
                            }
                        }
                        long num = new Date().getTime() - parse.getTime();
                        if (num>0){
                            list2.add(String.format("%.2f",Double.parseDouble(t.getTagvalue())));
                        }

                    }
                    /*for (int j = 0; j <nowTimeList.size(); j++) {
                        String time=nowTimeList.get(j);
                        String colltime=format+" "+time;
                        TagInfo tagInfo =new ReportFormServiceImpl().showDataByDateAndPoint2_2(code, date,colltime);
                        if(tagInfo !=null){
                            if (name==null){
                                name=tagInfo.getTagname2();
                            }
                            list2.add(String.format("%.2f",Double.parseDouble(tagInfo.getTagvalue())));
                        }else{
                            list2.add("0.00");
                        }
                    }*/
                    if (name == null) {
                        map2.put("tagName", "");
                    } else {
                        map2.put("tagName", name);
                    }
                    map2.put("tagCode", code);
                    map2.put("tagVlue", list2);
                    list.add(map2);
                }
            }
            resMap.put("data",list);
            resMap.put("code","0");
            resMap.put("msg","请求成功");
        } catch (Exception e) {
            resMap.put("data","");
            resMap.put("code","1");
            resMap.put("msg","请求失败,系统异常");
            e.printStackTrace();
        }
        System.out.println(JSON.toJSONString(resMap)+"---------实时数据曲线------");
        System.out.println(new Date()+"<-------------结束时间");
        return resMap;
    }



}
